home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Games / WHDLoad / Src / sources / files.i < prev    next >
Encoding:
Text File  |  1999-06-25  |  8.7 KB  |  419 lines

  1.  IFND FILES_I
  2. FILES_I=1
  3. ;*---------------------------------------------------------------------------
  4. ;  :Author.    Bert Jahn
  5. ;  :Contens.    macros for input/output via dos.library
  6. ;  :EMail.    wepl@kagi.com
  7. ;  :Address.    Franz-Liszt-Straße 16, Rudolstadt, 07404, Germany
  8. ;  :Version.    $Id: files.i 1.3 1999/06/24 23:13:31 jah Exp jah $
  9. ;  :History.    13.01.96 separated from WRip.asm
  10. ;        18.01.96 IFD Label replaced by IFD Symbol
  11. ;             because Barfly optimize problems
  12. ;        20.01.96 error string in _LoadFile changed
  13. ;             _LoadFile return register swapped
  14. ;        03.02.96 _AppendToFile added
  15. ;        18.05.96 returncode for _SaveFile & _SaveFileMsg
  16. ;        12.06.96 _SaveFileMsg uses now the name from _GetFileName
  17. ;             (otherwise difference between output and real file
  18. ;              if multiple assignments are used ie "C:LIST")
  19. ;        25.01.98 _CheckFileExist added
  20. ;  :Requires.    -
  21. ;  :Copyright.    This program is free software; you can redistribute it and/or
  22. ;        modify it under the terms of the GNU General Public License
  23. ;        as published by the Free Software Foundation; either version 2
  24. ;        of the License, or (at your option) any later version.
  25. ;        This program is distributed in the hope that it will be useful,
  26. ;        but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. ;        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28. ;        GNU General Public License for more details.
  29. ;        You can find the full GNU GPL online at: http://www.gnu.org
  30. ;  :Language.    68000 Assembler
  31. ;  :Translator.    Barfly V1.130
  32. ;---------------------------------------------------------------------------*
  33. *##
  34. *##    files.i
  35. *##
  36. *##    _GetFileName    filename(a0) -> fullname
  37. *##    _LoadFile    filename(a0) -> buffer(d0) buffersize(d1)
  38. *##    _LoadFileMsg    filename(a0) -> buffer(d0) buffersize(d1)
  39. *##    _SaveFile    buflen(d0) buffer(a0) filename(a1) -> success
  40. *##    _SaveFileMsg    buflen(d0) buffer(a0) filename(a1) -> success
  41. *##    _AppendOnFile    buflen(d0) buffer(a0) filename(a1)
  42. *##    _CheckFileExist    filename(a0) -> bool
  43.  
  44.     dc.b    "$Id: files.i 1.3 1999/06/24 23:13:31 jah Exp jah $"
  45.     EVEN
  46.  
  47.             INCLUDE    exec/memory.i
  48.         IFND    ERROR_I
  49.             INCLUDE    error.i
  50.         ENDC
  51.  
  52. ;----------------------------------------
  53. ; get full path+name from a file/dir
  54. ; Übergabe :    A0 = CPTR name of file
  55. ; Rückgabe :    D0 = CPTR full name / NIL
  56. ;             must freed via exec._FreeVec
  57.  
  58. GetFileName    MACRO
  59.     IFND    GETFILENAME
  60. GETFILENAME=1
  61. _GetFileName    movem.l    d2-d3/d6-d7/a6,-(a7)
  62.         moveq    #0,d6
  63.         
  64.         move.l    a0,d1
  65.         move.l    #ACCESS_READ,d2
  66.         move.l    (gl_dosbase,GL),a6
  67.         jsr    (_LVOLock,a6)
  68.         move.l    d0,d7            ;D7 = lock
  69.         beq    .nolock
  70.         
  71.         move.l    #256,d0
  72.         moveq    #MEMF_ANY,d1
  73.         move.l    (gl_execbase,GL),a6
  74.         jsr    (_LVOAllocVec,a6)
  75.         move.l    d0,d6            ;D6 = buffer
  76.         beq    .nobuf
  77.         
  78.         move.l    d7,d1
  79.         move.l    d6,d2
  80.         move.l    #256,d3
  81.         move.l    (gl_dosbase,GL),a6
  82.         jsr    (_LVONameFromLock,a6)
  83.         tst.l    d0
  84.         bne    .ok
  85.         move.l    d6,a1
  86.         move.l    (gl_execbase,GL),a6
  87.         jsr    (_LVOFreeVec,a6)
  88.         moveq    #0,d6
  89. .ok
  90. .nobuf
  91.         move.l    d7,d1
  92.         move.l    (gl_dosbase,GL),a6
  93.         jsr    (_LVOUnLock,a6)
  94. .nolock
  95.         move.l    d6,d0
  96.         movem.l    (a7)+,d2-d3/d6-d7/a6
  97.         rts
  98.     ENDC
  99.     ENDM
  100.  
  101. ;----------------------------------------
  102. ; Operation File
  103. ; Übergabe :    A0 = CPTR name of file
  104. ; Rückgabe :    D0 = APTR  loaded file (must freed via _FreeVec) OR NIL=ERROR
  105. ;        D1 = ULONG size of loaded file 
  106.  
  107. LoadFile    MACRO
  108.     IFND    LOADFILE
  109. LOADFILE=1
  110.     IFND    PRINTERRORDOS
  111.         PrintErrorDOS
  112.     ENDC
  113. _LoadFile    movem.l    d2-d7/a6,-(a7)
  114.         moveq    #0,d4                ;D4 = BOOL returncode
  115.         move.l    a0,d1                ;name
  116.         move.l    #MODE_OLDFILE,d2        ;mode
  117.         move.l    (gl_dosbase,GL),a6
  118.         jsr    (_LVOOpen,a6)
  119.         move.l    d0,d7                ;D7 = fh
  120.         bne    .openok
  121.         lea    (.readfile),a0
  122.         bsr    _PrintErrorDOS
  123.         bra    .erropen
  124. .openok
  125.         move.l    d7,d1                ;fh
  126.         moveq    #0,d2                ;offset
  127.         move.l    #OFFSET_END,d3            ;mode
  128.         move.l    (gl_dosbase,GL),a6
  129.         jsr    (_LVOSeek,a6)
  130.         tst.l    d0
  131.         bmi    .seekerr
  132.         move.l    d7,d1                ;fh
  133.         moveq    #0,d2                ;offset
  134.         move.l    #OFFSET_BEGINNING,d3        ;mode
  135.         jsr    (_LVOSeek,a6)
  136.         move.l    d0,d5                ;D5 = ULONG bufsize
  137.         beq    .errexamine
  138.         bpl    .sizeok
  139. .seekerr    lea    (.readfile),a0
  140.         bsr    _PrintErrorDOS
  141.         bra    .errexamine
  142. .sizeok
  143.         move.l    d5,d0
  144.         move.l    #MEMF_ANY,d1
  145.         move.l    (gl_execbase,GL),a6
  146.         jsr    (_LVOAllocVec,a6)
  147.         move.l    d0,d6                ;D6 = APTR buffer
  148.         bne    .memok
  149.         moveq    #0,d0
  150.         lea    (.notmem),a0
  151.         lea    (.readfile),a1
  152.         bsr    _PrintError
  153.         bra    .nomem
  154. .memok
  155.         move.l    d7,d1                ;fh
  156.         move.l    d6,d2                ;buffer
  157.         move.l    d5,d3                ;length
  158.         move.l    (gl_dosbase,GL),a6
  159.         jsr    (_LVORead,a6)
  160.         cmp.l    d5,d0
  161.         beq    .readok
  162.         lea    (.readfile),a0
  163.         bsr    _PrintErrorDOS
  164.         move.l    d6,a1
  165.         move.l    (gl_execbase,GL),a6
  166.         jsr    (_LVOFreeVec,a6)
  167.         bra    .readerr
  168.  
  169. .readok        move.l    d6,d4            ;returncode = size
  170. .readerr
  171. .nomem
  172. .errexamine        
  173.         move.l    d7,d1            ;fh
  174.         move.l    (gl_dosbase,GL),a6
  175.         jsr    (_LVOClose,a6)
  176. .erropen
  177.         move.l    d4,d0            ;returncode = bufptr
  178.         move.l    d5,d1            ;size
  179.         movem.l    (a7)+,d2-d7/a6
  180.         rts
  181.  
  182. .readfile    dc.b    "read file",0
  183. .notmem        dc.b    "not enough memory",0
  184.     EVEN
  185.     ENDC
  186.     ENDM
  187.  
  188. ;----------------------------------------
  189. ; gives message out and load file
  190. ; Übergabe :    A0 = CPTR name of file
  191. ; Rückgabe :    D0 = APTR  loaded file (must freed via _FreeVec) OR NIL=ERROR
  192. ;        D1 = ULONG size of loaded file 
  193.  
  194. LoadFileMsg    MACRO
  195.     IFND    LOADFILEMSG
  196. LOADFILEMSG=1
  197.     IFND    GETFILENAME
  198.         GetFileName
  199.     ENDC
  200.     IFND    LOADFILE
  201.         LoadFile
  202.     ENDC
  203. _LoadFileMsg    movem.l    d2-d3/a6,-(a7)
  204.         move.l    a0,d2
  205.  
  206.         bsr    _GetFileName
  207.         move.l    d0,d3
  208.         bne    .fine
  209.         move.l    d2,d0
  210. .fine
  211.         lea    (.loadfile),a0
  212.         move.l    d0,-(a7)
  213.         move.l    a7,a1
  214.         bsr    _PrintArgs
  215.         addq.l    #4,a7
  216.         
  217.         tst.l    d3
  218.         beq    .nofull
  219.         move.l    d3,a1
  220.         move.l    (gl_execbase,GL),a6
  221.         jsr    (_LVOFreeVec,a6)
  222. .nofull
  223.         move.l    d2,a0
  224.         bsr    _LoadFile
  225.         
  226.         movem.l    (a7)+,d2-d3/a6
  227.         rts
  228.  
  229. .loadfile    dc.b    "loading file ",155,"1m%s",155,"22m",10,0
  230.     EVEN
  231.     ENDC
  232.     ENDM
  233.  
  234. ;----------------------------------------
  235. ; Übergabe :    D0 = ULONG size of buffer
  236. ;        A0 = APTR  buffer
  237. ;        A1 = CPTR  name of file
  238. ; Rückgabe :    D0 = BOOL  success
  239.  
  240. SaveFile    MACRO
  241.     IFND    SAVEFILE
  242. SAVEFILE=1
  243.     IFND    PRINTERRORDOS
  244.         PrintErrorDOS
  245.     ENDC
  246. _SaveFile    movem.l    d2-d7/a6,-(a7)
  247.         move.l    a0,d7            ;D7 = buffer
  248.         move.l    d0,d6            ;D6 = size
  249.         moveq    #0,d4            ;D4 = return
  250.  
  251.         move.l    a1,d1    
  252.         move.l    #MODE_NEWFILE,d2
  253.         move.l    (gl_dosbase,GL),a6
  254.         jsr    (_LVOOpen,a6)
  255.         move.l    d0,d5            ;D5 = fh
  256.         bne    .openok
  257.         lea    (.writefile),a0
  258.         bsr    _PrintErrorDOS
  259.         bra    .erropen
  260. .openok
  261.         move.l    d5,d1
  262.         move.l    d7,d2
  263.         move.l    d6,d3
  264.         jsr    (_LVOWrite,a6)
  265.         moveq    #-1,d4
  266.         cmp.l    d6,d0
  267.         beq    .writeok
  268.         lea    (.writefile),a0
  269.         bsr    _PrintErrorDOS
  270.         moveq    #0,d4
  271. .writeok
  272.         move.l    d5,d1
  273.         jsr    (_LVOClose,a6)
  274.         
  275. .erropen    move.l    d4,d0
  276.         movem.l    (a7)+,d2-d7/a6
  277.         rts
  278. .writefile    dc.b    "write file",0
  279.     EVEN
  280.     ENDC
  281.     ENDM
  282.  
  283. ;----------------------------------------
  284. ; gives message out and save file
  285. ; Übergabe :    D0 = ULONG size of buffer
  286. ;        A0 = APTR  buffer
  287. ;        A1 = CPTR  name of file
  288. ; Rückgabe :    D0 = BOOL  success
  289.  
  290. SaveFileMsg    MACRO
  291.     IFND    SAVEFILEMSG
  292. SAVEFILEMSG=1
  293.     IFND    GETFILENAME
  294.         GetFileName
  295.     ENDC
  296.     IFND    SAVEFILE
  297.         SaveFile
  298.     ENDC
  299. _SaveFileMsg    movem.l    d2-d5/a6,-(a7)
  300.         move.l    d0,d4            ;D4 = bufsize    (d0)
  301.         move.l    a0,d3            ;D3 = buffer    (a0)
  302.         move.l    a1,d2            ;D2 = name    (a1)
  303.         
  304.         move.l    d2,a0
  305.         bsr    _GetFileName
  306.         move.l    d0,d5
  307.         beq    .sorry
  308.         move.l    d0,d2
  309. .sorry
  310.         lea    (.savefile),a0
  311.         move.l    d2,-(a7)
  312.         move.l    a7,a1
  313.         bsr    _PrintArgs
  314.         addq.l    #4,a7
  315.         
  316.         move.l    d4,d0
  317.         move.l    d3,a0
  318.         move.l    d2,a1
  319.         bsr    _SaveFile
  320.         move.l    d0,d2            ;returncode in D2 !!
  321.  
  322.         tst.l    d5
  323.         beq    .nofull
  324.         move.l    d5,a1
  325.         move.l    (gl_execbase,GL),a6
  326.         jsr    (_LVOFreeVec,a6)
  327. .nofull
  328.         move.l    d2,d0
  329.         movem.l    (a7)+,d2-d5/a6
  330.         rts
  331.  
  332. .savefile    dc.b    "save file ",155,"1m%s",155,"22m",10,0
  333.     EVEN
  334.     ENDC
  335.     ENDM
  336.  
  337. ;----------------------------------------
  338. ; Übergabe :    D0 = ULONG size of buffer
  339. ;        A0 = APTR  buffer
  340. ;        A1 = CPTR  name of file
  341. ; Rückgabe :    -
  342.  
  343. AppendOnFile    MACRO
  344.     IFND    APPENDONFILE
  345. APPENDONFILE=1
  346.     IFND    PRINTERRORDOS
  347.         PrintErrorDOS
  348.     ENDC
  349. _AppendOnFile    movem.l    d2-d7/a6,-(a7)
  350.         move.l    a0,d7            ;D7 = buffer
  351.         move.l    d0,d6            ;D6 = size
  352.  
  353.         move.l    a1,d1    
  354.         move.l    #MODE_READWRITE,d2
  355.         move.l    (gl_dosbase,GL),a6
  356.         jsr    (_LVOOpen,a6)
  357.         move.l    d0,d5            ;D5 = fh
  358.         bne    .openok
  359.         lea    (.writefile),a0
  360.         bsr    _PrintErrorDOS
  361.         bra    .erropen
  362. .openok
  363.         move.l    d5,d1                ;fh
  364.         moveq    #0,d2                ;offset
  365.         move.l    #OFFSET_END,d3            ;mode
  366.         move.l    (gl_dosbase,GL),a6
  367.         jsr    (_LVOSeek,a6)
  368.         tst.l    d0
  369.         bpl    .seekok
  370.         lea    (.writefile),a0
  371.         bsr    _PrintErrorDOS
  372.         bra    .seekerr
  373. .seekok
  374.         move.l    d5,d1
  375.         move.l    d7,d2
  376.         move.l    d6,d3
  377.         jsr    (_LVOWrite,a6)
  378.         cmp.l    d6,d0
  379.         beq    .writeok
  380.         lea    (.writefile),a0
  381.         bsr    _PrintErrorDOS
  382. .writeok
  383. .seekerr
  384.         move.l    d5,d1
  385.         jsr    (_LVOClose,a6)
  386.         
  387. .erropen    movem.l    (a7)+,d2-d7/a6
  388.         rts
  389. .writefile    dc.b    "write file",0
  390.     EVEN
  391.     ENDC
  392.     ENDM
  393.  
  394. ;----------------------------------------
  395. ; Übergabe :    A0 = CPTR filename
  396. ; Rückgabe :    D0 = BOOL exist
  397.  
  398. CheckFileExist    MACRO
  399.     IFND    CHECKFILEEXIST
  400. CHECKFILEEXIST=1
  401. _CheckFileExist    movem.l    d2/a6,-(a7)
  402.         move.l    a0,d1
  403.         move.l    #ACCESS_READ,d2
  404.         move.l    (gl_dosbase,GL),a6
  405.         jsr    (_LVOLock,a6)
  406.         move.l    d0,d1
  407.         beq    .end
  408.         jsr    (_LVOUnLock,a6)
  409.         moveq    #-1,d0
  410. .end        movem.l    (a7)+,_MOVEMREGS
  411.         rts
  412.     ENDC
  413.     ENDM
  414.  
  415. ;----------------------------------------
  416.  
  417.  ENDC
  418.  
  419.